package com.wf.smartcomm.server.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.job.ScheduleJob;
import com.wf.smartcomm.server.service.ScheduleService;
import com.wf.smartcomm.server.util.IdUtils;

@Service
@Transactional
public class ScheduleServiceImpl implements ScheduleService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void add(ScheduleJob job) {
		job.setObjectId(IdUtils.uuid());
		job.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().persist(job);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public ScheduleJob get(String jobId) {
		Query query = factory.getCurrentSession().createQuery("from ScheduleJob where jobId =:jobId");
		query.setParameter("jobId", jobId);
		return (ScheduleJob) query.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<ScheduleJob> list() {
		Query query = factory.getCurrentSession().createQuery("from ScheduleJob");
		return query.list();
	}

	@Override
	public void update(ScheduleJob job) {
		job.setUpdatedAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().merge(job);
	}

}
